/*
 * @Descripttion:
 * @version:
 * @Author: gengzhi
 * @Date: 2020-08-20 14:16:23
 * @LastEditors: gengzhi
 * @LastEditTime: 2020-08-20 15:26:07
 */
package main

import (
	"flag"
	"fmt"
	"go2o/app"
	"go2o/core"
	"log"
	"os"
	"runtime"
)

func main() {
	var (
		ch        = make(chan bool)
		confFile  string
		port      int
		apiPort   int
		kafkaAddr string
		debug     bool
		trace     bool
		runDaemon bool
		help      bool
		showVer   bool
		newApp    *core.AppImpl
		appFlag   = app.FlagWebApp
	)
	defaultMqAddr := os.Getenv("GO2O_KAFKA_ADDR")
	if len(defaultMqAddr) == 0 {
		defaultMqAddr = "127.0.0.1:4222"
	}
	flag.IntVar(&port, "port", 1427, "thrift service port")
	flag.IntVar(&apiPort, "apiport", 1428, "api service port")
	flag.BoolVar(&debug, "debug", false, "enable debug")
	flag.BoolVar(&trace, "trace", false, "enable trace")
	flag.BoolVar(&help, "help", false, "command usage")
	flag.StringVar(&confFile, "conf", "app.conf", "")
	flag.StringVar(&kafkaAddr, "mqs", defaultMqAddr,
		"mq cluster address, like: 192.168.1.1:4222,192.168.1.2:4222")
	flag.BoolVar(&runDaemon, "d", false, "run daemon")
	flag.BoolVar(&showVer, "v", false, "print version")
	flag.Parse()

	if runDaemon {
		appFlag = appFlag | app.FlagDaemon
	}
	appFlag = appFlag | app.FlagRpcServe
	if help {
		flag.Usage()
		return
	}
	if showVer {
		fmt.Println(fmt.Sprintf("go2o version v%s", core.Version))
		return
	}
	log.SetOutput(os.Stdout)
	log.SetFlags(log.LstdFlags | log.Ltime | log.Ldate | log.Lshortfile)
	runtime.GOMAXPROCS(runtime.NumCPU())
	newApp = core.NewApp(confFile)
	if debug {
		go app.AutoInstall()
	}
	if !core.SignalNotify(ch)
	gof.CurrentApp=newApp
	cache.Initialize()
}
